<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles" %>
<%@ taglib uri="/WEB-INF/vectrics.tld"    prefix="vectrics" %>


<%@ page import="org.vectrics.hibernate.ThreadProperties" %>
<%@ page import="org.vectrics.domain.Domain" %>
<%@ page import="java.util.Collection" %>
<%@ page import="org.vectrics.recruiter.RecruitServiceLocator" %>

<script type="text/javascript" src="/hrs-public/custom/js/vectrics.js"></script>
<script type="text/javascript" src="/hrs-public/custom/js/logging.js"></script>

<script type="text/javascript" src="/hrs-public/custom/ajaxtk/xml/AjxXmlDoc.js"></script>
<script type="text/javascript" src="/hrs-public/custom/ajaxtk/core/AjxCore.js"></script>
<script type="text/javascript" src="/hrs-public/custom/ajaxtk/core/AjxEnv.js"></script>
<script type="text/javascript" src="/hrs-public/custom/ajaxtk/core/AjxException.js"></script>
<script type="text/javascript" src="/hrs-public/custom/ajaxtk/core/AjxImg.js"></script>
<script type="text/javascript" src="/hrs-public/custom/ajaxtk/util/AjxCallback.js"></script>
<script type="text/javascript" src="/hrs-public/custom/ajaxtk/util/AjxTimedAction.js"></script>
<script type="text/javascript" src="/hrs-public/custom/ajaxtk/net/AjxPost.js"></script>

<%
	String domainId = ThreadProperties.getUriDomainId();
    Domain domain = ThreadProperties.getDomain();
    String dominioId = domain.getId().toString();
	String experienceAddURL = "/hrs-public/cv/" + domain + "/experienceAdd.do";
	String userId =request.getParameter("personId");
	String contextPath = request.getContextPath();
	String userName = ThreadProperties.getPerson().getUser().getUserName();
	String password = ThreadProperties.getPerson().getUser().getPassword();
 
	// Actividades de las companias 
    Collection allCompanyActivity = null;
    //Recupero todos las actividades de las companias para listarlas en la pagina.
	allCompanyActivity = RecruitServiceLocator.getPersonService().findAllCompanyActivity();
    //seteo la request las Actividades de las companias. 
	request.setAttribute("allCompanyActivity", allCompanyActivity);
    
    // Jerarquias
    Collection allHierarchy = null;
    //Recupero todos Jerarquias para listarlas en la pagina.
	allHierarchy = RecruitServiceLocator.getPersonService().findAllHierarchy();
    //seteo la request las Jerarquias 
	request.setAttribute("allHierarchy", allHierarchy);
    
    // Areas
    Collection allArea = null;
    //Recupero todos Areas para listarlas en la pagina.
	allArea = RecruitServiceLocator.getPersonService().findAllArea();
    //seteo la request las Areas 
	request.setAttribute("allArea", allArea);
    
	Long idArea = new Long(1l);
 
    Collection allDepartment = null;
    //Recupero todos Departamentos para listarlas en la pagina.
	allDepartment=RecruitServiceLocator.getPersonService().findAllDepartment(idArea);
    //seteo la request las Departamentos 
	request.setAttribute("allDepartment", allDepartment);
    
	String experienceId =request.getParameter("experienceId");
	String formAction ="";
	if ((experienceId!=null) && (!experienceId.trim().equals(""))){
		formAction = "/cv/" + domainId + "/saveExperience.do?personId="+ userId + "&experienceId=" + experienceId;
	} else {
		formAction = "/cv/" + domainId + "/saveExperience.do?personId="+ userId;
	}
%>



<script type="text/javascript">

function findDepartment() { 
    var area = document.getElementById("idArea");
	log('findDepartment() - idArea: ' + area.value);
	var href = '/hrs-public/ws?command=get.cv.departament&user=<%=userName%>&domainId=<%=dominioId%>&pcode=<%=password%>';
	href += '&areaId=' + area.value;
		
	log('findMatches() - href: ' + href);
	remoteCall(href, handleXmlSearchResponse);
 } 

function enviarValor(){
	findDepartment();
}

function displayDepartmentNodes(departments) {
	var comboDepartment = document.getElementById("idDepartment");
	log(comboDepartment); 
	for (loop=0; loop < comboDepartment.options.length; loop++){
		comboDepartment.options[loop] = null;	
	}
	comboDepartment.options.length=0;
	log(comboDepartment);
	for (i = 0; i < departments.length; i++) { 
		node = departments[i];
		var id =  node.getAttribute("id");
		var desc =  node.getAttribute("description");
		log(desc); 
		add(comboDepartment, id , desc);
	}
}

function add(combo, valor, text) {
    elem = combo.options.length;
    var selOpcion = new Option();
    selOpcion.text = text;
    selOpcion.value = valor;
    combo.options[elem]=selOpcion;
} 

function handleXmlSearchResponse() {
		  // if the readyState code is 4 (Completed)
		  // and http status is 200 (OK) we go ahead and get the responseText
		  // other readyState codes:
		  // 0=Uninitialised 1=Loading 2=Loaded 3=Interactive
		  log("Response received from server, processing XML response.");
		  var xmlhttp = getXmlHttp();
		  if (xmlhttp.readyState == 4) {
				if (xmlhttp.status == 200) {
						xmlDoc = AjxXmlDoc.createFromXml(xmlhttp.responseText);
						nativeDocument = xmlDoc.getDoc();
						department = nativeDocument.getElementsByTagName("department");
						displayDepartmentNodes(department);
				} else {
					log("Not processed - Response status = " + xmlhttp.status);
				}
		  } else {
			  log("Not processed - Response readyState = " + xmlhttp.readyState);
		  }
}


</script>

<jsp:useBean 	id="experienceForm" 
				class="org.vectrics.recruiter.struts.cv.ExperienceForm" 
				scope="request"/>

<table width="500">
	<tr>
		<td valign="middle" >
			<html:form action="<%=formAction%>" focus="company" method="POST" >

				<table width="100%">
					<tr>
						<td valign="middle"  width="100%">
							<table width="100%">
								<tr>
									<td valign="top"  width="50%">
										<font class="formLabelClass"><bean:message key="prompt.experience.company" /><b> *</b></font>
										<vectrics:errors property="company"/>
									</td>
									<td valign="top" width="50%" >
									    <html:text styleClass="formEditClass" property="company" maxlength="40"/>
									</td>
								</tr>
								<tr>
									<td valign="top" >
										<font class="formLabelClass"><bean:message key="prompt.experience.companyActivity" /><b> *</b></font>
									    <vectrics:errors property="companyActivity"/>
									</td>
									<td valign="top" >
								       <html:select 
												name="experienceForm" 
												property="companyActivity" 
												styleClass="formComboClass"
												 >
											  <html:options
											  		collection="allCompanyActivity" 
											  		labelProperty="description" 
											  		property="id" />
										</html:select> 
									</td>
								</tr>
								<tr>
									<td  valign="top">
										<font class="formLabelClass"><bean:message key="prompt.experience.national"/><b> *</b></font>
							            <vectrics:errors property="national"/>
									</td>
									<td  valign="top">
										<html:select property="national" styleClass="formComboClass">
											<html:option value="n"><bean:message key="prompt.national"/></html:option>
											<html:option value="i"><bean:message key="prompt.international"/></html:option>
										</html:select>
									 </td>
					            </tr>
								<tr>
									<td valign="top" >
										<font class="formLabelClass"><bean:message key="prompt.experience.hierarchy"/><b> *</b></font>
										<vectrics:errors property="hierarchy"/>
									</td>
									<td valign="top" >
										<html:select 
												name="experienceForm" 
												property="hierarchy" 
												styleClass="formComboClass"
												 >
											  <html:options
											  		collection="allHierarchy" 
											  		labelProperty="description" 
											  		property="id" />
										</html:select> 
									</td>
								</tr>
								<tr>
									<td valign="top" >
										<font class="formLabelClass"><bean:message key="prompt.experience.area"/><b> *</b></font>
										<vectrics:errors property="department"/>
									</td>
									<td valign="top" >
										<html:select 
												name="experienceForm" 
												property="area" 
												styleClass="formComboClass"
												onchange="enviarValor();"
												styleId="idArea"
												 >
											  <html:options
											  		collection="allArea" 
											  		labelProperty="description" 
											  		property="id" />
										</html:select> 
									</td>
								</tr>						 
								<tr>
									<td valign="top" >
										<font class="formLabelClass"><bean:message key="prompt.experience.department"/><b> *</b></font>
										<vectrics:errors property="department"/>
									</td>
									<td valign="top" >
										<html:select 
												name="experienceForm" 
												property="department" 
												styleClass="formComboClass"
												styleId="idDepartment" 
												 >
											  <html:options
											  		collection="allDepartment" 
											  		labelProperty="description" 
											  		property="id" />
										</html:select> 
									</td>
								</tr>	
					            <tr>
									<td valign="top">
										<font class="formLabelClass"><bean:message key="prompt.experience.post"/><b> *</b></font>
										<vectrics:errors property="post"/>
									</td>
									<td valign="top">
										<html:text property="post" styleClass="formEditClass" />
									</td>
								</tr>
								<tr>
									<td valign="top">
										<font class="formLabelClass"><bean:message key="prompt.experience.dateIncome"/><b> *</b></font>
										<vectrics:errors property="dateIncome"/>
									</td>
									<td  valign="top">
										<html:text property="dateIncome" styleClass="formButton" styleId="dateIncome" />
										<input type="button" id="dateIncomeBt" name="dateIncomeBt" value="..." /> 
										<script type="text/javascript">
  												Calendar.setup(
    												{
      												inputField  : "dateIncome",         // ID of the input field
      												ifFormat    : "%y/%m/%d",    // the date format
      												button      : "dateIncomeBt"       // ID of the button
    												}
  												);
										</script>
									</td>
					            </tr>
                                <tr>
									<td valign="top">
										<font class="formLabelClass"><bean:message key="prompt.experience.staffCharge"/></font>
										<vectrics:errors property="staffCharge"/>
									</td>
									<td valign="top">
										<html:text property="staffCharge" styleClass="formEditClass" maxlength="4"/>
									</td>
						   	   </tr>
						   	   <tr>
									<td valign="top">
										<font class="formLabelClass"><bean:message key="prompt.experience.dateDischarge"/></font>
										<vectrics:errors property="dateDischarge"/>
									</td>
									<td valign="top">
										<html:text property="dateDischarge" styleClass="formButton" styleId="dateDischarge" />
										<input type="button" id="dateDischargeBt" name="dateDischargeBt" value="..." /> 
										<script type="text/javascript">
  												Calendar.setup(
    												{
      												inputField  : "dateDischarge",         // ID of the input field
      												ifFormat    : "%y/%m/%d",             // the date format
      												button      : "dateDischargeBt"       // ID of the button
    												}
  												);
										</script>
									</td>
						   	   </tr>
						   	   <tr>
									<td valign="top">
										<font class="formLabelClass"><bean:message key="prompt.experience.work"/></font>
										<vectrics:errors property="work"/>
									</td>
									<td valign="top">
										<html:textarea property="work" styleClass="formEditClass" />
									</td>
						   	   </tr>
						   	   <tr>
									<td valign="top">
										<font class="formLabelClass"><bean:message key="prompt.experience.knowledge"/></font>
										<vectrics:errors property="knowledge"/>
									</td>
									<td valign="top">
										<html:textarea property="knowledge" styleClass="formEditClass" />
									</td>
						   	   </tr>					   	   
								<tr>
									<td valign="top" colspan="2">
										<table>
											<tr>
												<td valign="top"  align="left"></td>
												<td valign="top"  align="left">
													<vectrics:formButton name="submit" code="public.button.ok" phraseCode="button.ok" />
													&nbsp;
													<vectrics:formButton name="cancel" code="public.button.cancel" phraseCode="button.cancel"/>
												</td>
											</tr>
										</table>
									</td>
								</tr>
							</table>
						</td>
					</tr>
				</table>
			</html:form>
		</td>
	</tr>
</table>


